﻿<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'user_base';
</cfscript>

<!---登录--->
<cffunction name="login" access="remote" returnFormat="json">
<!---<cfscript>
mobile = 13402679077;
IMSI=8696300013001155;
clientplat='andr';
sourceacc='chinalaohu';
userpwd='E72f4545eb68c96c754f91fc01573517';
key = hash(mobile & IMSI & clientplat & sourceacc & dateformat(now(),'yyyymmdd'),'md5');
</cfscript>
<cfoutput>
<a href="http://adio.chinalaohu.com/app/cfc/userlogin.cfc?method=login&
mobile=#mobile#&IMSI=#IMSI#&clientplat=#clientplat#&sourceacc=#sourceacc#&key=#key#
">
click
</a></cfoutput><br>--->

  <cfscript>
	success = 0;
	j = {"success":"0"};
	m_auth = 0;
	</cfscript>
  <cfparam name="errCounts" default="0" type="integer">
  <cfif isdefined('mobile') and isdefined('userpwd')>
  	<cfif isdefined('key')>
    	<cfinvoke component="userlogin" method="checkRegKey" returnvariable="checkRegKey" />
      <cfif checkRegKey is true>
				<cfscript>
				//,"msg":"KEY通过"
        //j = {"success":"1","errCode":"2"};
        </cfscript>
        
        
        <!---出现验证码--->
        <cfif errCounts gte 3 and isdefined('valCode')>
          <cfquery datasource="#DB#" name="checkloginvcode">
          select vcode
          from user_logvcode
          where mobile = '#mobile#'
          </cfquery>
          <cfif checkloginvcode.vcode neq valCode>
            <cfscript>
						//,"msg":"验证码错误"
            j = {"success":"0","errCode":"1"};
            </cfscript>
          <cfelse>
            <cfset vcodesucc = 1>
          </cfif>

        </cfif>
        
        
        
        <cfif errCounts lt 3 or isdefined('vcodesucc')>
					<!---密码是否正确--->
          <cfquery datasource="#DB#" name="checkuspwd">
          SELECT uid,uuid,userpwd,muuid
          FROM #dbt#
          WHERE mobile = '#mobile#'
          </cfquery>
          <cfif checkuspwd.recordcount eq 0>
            <cfscript>
						//,"msg":"手机号未注册"
            j = {"success":"0","errCode":"3"};
            </cfscript>
          <cfelse>
            <cfif checkuspwd.userpwd eq userpwd>
            	<cfquery datasource="#DB#">
              delete from user_logvcode
              where mobile = '#mobile#'
              </cfquery>
             	<!---临时固定孟兵手机号UUID--->
				<!--- 
              <cfif mobile eq '13402679077'>
              	<cfset newmuuid = 'D99B5F71-DFE7-71BA-FE306A9736567C9E'>
              <cfelse>
              <cfset newmuuid = CREATEUUID()>
              <!---针对手机生成随机UUID--->
              <cfquery datasource="#DB#">
              update #dbt# set
              muuid = '#newmuuid#'
              where uid = '#checkuspwd.uid#'
              </cfquery>
              
              </cfif>
			  --->
			  <cfset newmuuid = CREATEUUID()>
              <cfquery datasource="#DB#">
              update #dbt# set
              muuid = '#newmuuid#'
              where uid = '#checkuspwd.uid#'
              </cfquery>
			  
			  <!--- 插入登入日志 --->
			  <cfquery datasource="#DB#">
					insert into user_login (uid,login
				<cfif isdefined('clientplat') and len(clientplat) gt 0>
					,plat
				</cfif>
				<cfif isdefined('imsi') and len(imsi) gt 0>
					,imsi
				</cfif>
					,loginDT) values('#checkuspwd.uid#','#mobile#'
				<cfif isdefined('clientplat') and len(clientplat) gt 0>
					,'#clientplat#'
				</cfif>
				<cfif isdefined('imsi') and len(imsi) gt 0>
					,'#imsi#'
				</cfif>
					,#now()#)
			  </cfquery>
			  
              <cfscript>
              j = {"success":"1","uuid": newmuuid};
              </cfscript>
              
            <cfelse>
              <!---密码出错次数--->
              <cfset errorcounts = errCounts + 1>
              <cfquery datasource="#DB#" name="checkerrorcounts">
              select counts 
              from user_logvcode
              where mobile = '#mobile#'
              </cfquery>
              <cfif checkerrorcounts.recordcount eq 0>
                <cfquery datasource="#DB#">
                insert into user_logvcode(mobile,counts,dt)
                value('#mobile#',#errorcounts#,#now()#)
                </cfquery>
              
              <cfelse>
                <cfquery datasource="#DB#">
                update user_logvcode set
                counts = #errorcounts#,
                dt = #now()#
                where mobile = '#mobile#'              
                </cfquery>
              </cfif>
                      
              <cfscript>
							//,"msg":"密码错误"
              j = {"success":"0","errorcounts":errorcounts,"errCode":"3"};
              </cfscript>
            </cfif>
            
          </cfif>
        </cfif>
      <cfelse>
				<cfscript>
				//mobile + imsi + clientplat + sourceacc + (new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
				//okey = mobile & IMSI & clientplat & sourceacc & dateformat(now(),'yyyymmdd');
				//vkey = hash(okey,'md5');
        //,"msg":"KEY不正确"
        j = {"success":"0","errCode":"2"};
        </cfscript>
      </cfif>
      
    <cfelse>
			<cfscript>
      //,"msg":"缺少KEY"	
      j = {"success":"0","errCode":"-1"};
      </cfscript>
  
    </cfif>


	<cfelse>
		<cfscript>
		//,"msg":"缺少参数"
    j = {"success":"0","errCode":"-1"};
    </cfscript>
  
  </cfif>
  
  
  
  <cfscript>
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>

</cffunction>


<cffunction name="checkRegKey" access="public" returntype="boolean">
	<cfscript>
	if (isdefined('mobile') and isdefined('IMSI') and isdefined('sourceacc') and isdefined('key')){
		//验证字段(手机号+IMSI+平台+来源+日期天 的MD5)
		vkey = hash(mobile & IMSI & clientplat & sourceacc,'md5');
		if (key eq vkey){
			return true;
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
	</cfscript>
  <!---<cfif isdefined('mobile') and isdefined('IMEI') and isdefined('sourceacc')>
  	<cfreturn true>
  <cfelse>
  	<cfreturn false>
  </cfif>--->

</cffunction>


<!---获取图片验证码--->
<cffunction name="getCaptcha" access="remote" returntype="string" returnFormat="plain">
	
	<cfscript>
	vcode = randrange(1000,9999);
	captchaimage = GetTempDirectory() & 'captcha' & vcode & '.png';
	</cfscript>
<cfimage action="captcha" width="88" height="40" text="#vcode#" destination="#captchaimage#" difficulty="low" fontSize="20" overwrite="yes">
	<cfscript>
  photoBinary = FileReadBinary(captchaimage);
  </cfscript>
  <cfif isdefined('mobile')>
  <cfquery datasource="#DB#">
  replace into user_logvcode(mobile,vcode,dt)
  value('#mobile#','#vcode#',#now()#)
  </cfquery>
  </cfif>
  
  
<!---      <cfscript>
vcode = '8888';
captchaimage = GetTempDirectory() & 'captcha' & vcode & '.png';
			</cfscript>
<cfimage action="captcha" width="88" height="40" text="#vcode#" destination="#captchaimage#" difficulty="low" fontSize="20" overwrite="yes">
      <cfscript>
			photoBinary = FileReadBinary(captchaimage);
			</cfscript>
      <cfoutput>
      <img
src="data:image/*;base64,#toBase64( photoBinary )#"
/></cfoutput>--->
	<!---<cfoutput>data:image/*;base64,#toBase64( photoBinary )#</cfoutput>--->
  <!---<cfoutput>#toBase64(photoBinary)#</cfoutput>--->
  <cfreturn toBase64(photoBinary)>
</cffunction>








<cffunction access="remote" name="vcodeReg" returnFormat="json">
	<cfscript>
  mobile = '13402679077';
  success = 0;
  rs = {"success": success};
  </cfscript>
  <!---判断手机号是否注册--->
  <cfquery datasource="#DB#" name="checkmobile">
  select uid,uuid
  from #dbt#
  where mobile = '#mobile#'
  </cfquery>
  <cfif checkmobile.recordcount eq 0>
  	<!---验证码输入--->
    <cfinvoke component="cfc.plat.user_regvcode" method="checkvcode" mobile="#mobile#" returnvariable="checkvcode" />
    <cfif checkvcode.recordcount eq 0>
    	<!---验证码不存在或已经过期--->
			<cfscript>
			success = 2;
			rs = {"success": success};
      </cfscript>
    <cfelseif vcode neq checkvcode.vcode>
    	<cfif checkvcode.counts gte 3>
				<cfscript>
        success = 3;
        rs = {"success": success};
        </cfscript>
        <cfquery datasource="#DB#">
        delete from user_regvcode
        where mobile = '#mobile#'
        </cfquery>
      <cfelse>
        <cfquery datasource="#DB#">
        update user_regvcode set
        counts = counts + 1
        where mobile = '#mobile#'
        </cfquery>
				<cfscript>
        success = 0;
        rs = {"success": success};
        </cfscript>
      </cfif>
    <cfelse>
      <!---正式注册--->
      <cfquery datasource="#DB#" result="addrs">
      insert into #dbt#(create_dt,mobile)
      values(#now()#,'#mobile#')
      </cfquery>
			<cfscript>
      success = 1;
      rs = {"success": success, "lhid": addrs.GENERATEDKEY};
      </cfscript>
    </cfif>
  <cfelse>
		<cfscript>
    success = 0;
    rs = {"success": success, "lhid":checkmobile.uid};
    </cfscript>
  </cfif>
  <cfreturn rs>
</cffunction>



<cffunction name="myFunction" access="public" returntype="string">
  <cfargument name="myArgument" type="string" required="yes">
  <cfset myResult="foo">
  <cfreturn myResult>
</cffunction>
  
</cfcomponent>